<?php
//后台用户管理
namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

//密码验证

class UserController extends BaseController
{


    //用户列表
    public function index()
    {
         //config('page.pagesize');
        //分页
        $data = User::withTrashed()->paginate($this->pagesize);

        return view('admin.user.index',compact('data'));
    }
    //添加显示
    public function create()
    {
        return view('admin.user.create');
    }

    //添加处理
    public function store(Request $request)
    {
        //表单验证
        $this->validate($request,[
           'username'=>'required|unique:users,username',
           'truename'=>'required',
           'password'=>'required|confirmed',
           'phone'=>'nullable|phone'
        ],
        [
            'phone.phone'=>'手机号不合法'
        ]);
        //获取表单数据
        $post = $request->except(['_token','password_confirmation']);
        //添加用户入库
        $userModel = User::create($post);

        $pwd = $post['password'];
        //发邮件给用户
        \Mail::send('mail.useradd',compact('userModel','pwd'),function (\Illuminate\Mail\Message $message) use ($userModel){
            //发给谁
            $message->to($userModel->email);
            //主题
            $message->subject('开通账号邮件通知');
        });
        //跳转到列表页
        return redirect(route('admin.user.index'))->with('success','添加用户成功');
    }
    //删除用户操作
    public function del(int $id)
    {
        //软删除
        User::find($id)->delete();
        //硬删除
        //User::find($id)->forceDelete();
        return ['status'=>0,'msg'=>'删除成功'];
    }
    //还原
    public function restore(int $id)
    {

        User::onlyTrashed()->where('id',$id)->restore();
        return redirect(route('admin.user.index'))->with('success','还原成功');
    }
    //全选删除
    public function delall(Request $request)
    {
        $ids = $request->get('id');
        User::destroy($ids);
        return ['status'=>0,'msg'=>'全选删除成功'];
    }
    //修改显示
    public function edit(int $id)
    {
        $model = User::find($id);
        return view('admin.user.edit',compact('model'));
    }

    //修改处理
    public function update(Request $request,int $id)
    {
        $model = User::find($id);

        //原密码 明文
        $spass = $request->get('spassword');
        //原密码  密文
        $oldpass = $model->password;
        //检查是否一致
        $bool = Hash::check($spass,$oldpass);
        if($bool){
            //修改
            $data = $request->only(['truename','password','phone','sex','email']);
            if(!empty($data['password'])){
                $data['password']=bcrypt($data['password']);
            }else{
                unset($data['password']);
            }
            $model->update($data);
            return redirect(route('admin.user.index'))->with('success','修改用户成功');
        }
        return redirect(route('admin.user.edit',$model))->withErrors(['error'=>'原密码不正确']);

    }
    //分配角色用户处理
    public function role(Request $request,User $user)
    {
        //判断是否是post提示
        if($request->isMethod('post')){
            $post = $this->validate($request,[
               'role_id'=>'required'
            ],[
                'role_id.required'=>"必须选择"
            ]);
            $user->update($post);
            return redirect(route('admin.user.index'));
        }
        //读出所有角色
        $roleAll = Role::all();

        return view('admin.user.role',compact('user','roleAll'));



    }









}
